package com.chatmusicstatus.accounts;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.widget.Toast;
import com.chatmusicstatus.R;

public class ModifyAcc extends PreferenceActivity {

    AccountSQLiteHelper accHelper;
    
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        final int acc_id = getIntent().getIntExtra("acc_id", -1);
        if(acc_id==-1)
        {
            setResult(0);
            finish();
        }
        accHelper = new AccountSQLiteHelper(this);
        addPreferencesFromResource(R.xml.createaccountadv);
        PreferenceScreen account = (PreferenceScreen) findPreference("account");
        Preference delete = new Preference(this);
        delete.setKey("delete");
        delete.setTitle(R.string.deletetxt);
        delete.setPersistent(false);
        delete.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {

            public boolean onPreferenceClick(Preference prfrnc) {
                SQLiteDatabase db = accHelper.getWritableDatabase();
                db.delete(AccountSQLiteHelper.TABLE, AccountSQLiteHelper.ID + "=?", new String[] {Integer.toString(acc_id)});
                setResult(1);
                finish();
                return true;
            }
        });
        account.addPreference(delete);
        Cursor cursor = accHelper.getAccInfoFromID(acc_id);
        startManagingCursor(cursor);
        if(cursor.moveToNext())
        {
            ((EditTextPreference)findPreference("accname")).setText(cursor.getString(0));
            ((EditTextPreference)findPreference("uname")).setText(cursor.getString(3));
            ((EditTextPreference)findPreference("pass")).setText(cursor.getString(4));
            ((EditTextPreference)findPreference("server")).setText(cursor.getString(1));
            ((EditTextPreference)findPreference("port")).setText(cursor.getString(2));
            ((EditTextPreference)findPreference("service")).setText(cursor.getString(8));
            if(cursor.getInt(5)==1)
                ((CheckBoxPreference)findPreference("enable")).setChecked(true);
            else
                ((CheckBoxPreference)findPreference("enable")).setChecked(false);
        }
        else
        {
            Toast.makeText(this, R.string.accunavailable, Toast.LENGTH_LONG).show();
        }
        cursor.close();
        
        findPreference("save").setOnPreferenceClickListener(new OnPreferenceClickListener() {

            public boolean onPreferenceClick(Preference prfrnc) {
                SQLiteDatabase db = accHelper.getWritableDatabase();
                ContentValues accInfo = new ContentValues();
                accInfo.put(AccountSQLiteHelper.ANAME, ((EditTextPreference)findPreference("accname")).getText());
                accInfo.put(AccountSQLiteHelper.UNAME, ((EditTextPreference)findPreference("uname")).getText());
                accInfo.put(AccountSQLiteHelper.PASS, ((EditTextPreference)findPreference("pass")).getText());
                accInfo.put(AccountSQLiteHelper.SERVER, ((EditTextPreference)findPreference("server")).getText());
                accInfo.put(AccountSQLiteHelper.PORT, ((EditTextPreference)findPreference("port")).getText());
                accInfo.put(AccountSQLiteHelper.SERVICE, ((EditTextPreference)findPreference("service")).getText());
                if(((CheckBoxPreference)findPreference("enable")).isChecked())
                    accInfo.put(AccountSQLiteHelper.ENABLE, 1);
                else
                    accInfo.put(AccountSQLiteHelper.ENABLE, 0);
                accInfo.put(AccountSQLiteHelper.MESSAGE, "");
                accInfo.put(AccountSQLiteHelper.UTUNE, 2);
                db.update(AccountSQLiteHelper.TABLE, accInfo, AccountSQLiteHelper.ID + "=?", new String[] {Integer.toString(acc_id)});
                setResult(1);
                finish();
                return true;
            }
        });
    }
}
